#include "utility.h"
#include <QDir>

namespace ShTo {

bool Utility::ls(const char* psPath, List<std::string>::Type& zList,
                 int iMode, bool bFull)
{
    QDir dir(psPath);
    if (!dir.exists())
        return false;
    QFileInfoList entries = dir.entryInfoList(QDir::NoDotAndDotDot |
                                              QDir::Dirs | QDir::Files);
    int size = entries.size();
    for (int i = 0; i < size; ++i)
    {
        QFileInfo entry = entries.at(i);
        if (entry.isDir())
        {
            if (iMode == 0 || iMode == 1)
            {
                QString path = bFull ? entry.absolutePath() :
                                       entry.filePath();
                zList.push_back(qPrintable(path));
            }
        }
        else if (entry.isFile())
        {
            if (iMode == 0 || iMode == 2)
            {
                QString path = bFull ? entry.absoluteFilePath() :
                                       entry.fileName();
                zList.push_back(qPrintable(path));
            }
        }
    }
    return true;
}

bool Utility::isDir(const char* psPath)
{
    QFileInfo info(psPath);
    return info.isDir();
}

bool Utility::mkDir(const char* psPath)
{
    QDir dir;
    return dir.mkpath(psPath);
}

bool Utility::rmDir(const char* psPath)
{
    QDir dir(psPath);
    QFileInfoList entries = dir.entryInfoList();
    int size = entries.size();
    for (int i = 0; i < size; ++i)
    {
        QFileInfo entry = entries.at(i);
        if (entry.isDir())
        {
            if (!rmDir(qPrintable(entry.absolutePath())))
                return false;
        }
        else
        {
            if (!dir.remove(entry.fileName()))
                return false;
        }
    }
    return dir.rmdir(psPath);
}

bool Utility::mvDir(const char* psPath, const char* psDest)
{
    if (cpDir(psPath, psDest))
        return rmDir(psPath);
    return false;
}

bool Utility::cpDir(const char* psPath, const char* psDest)
{
    if (!mkDir(psDest))
        return false;
    QDir dir(psPath);
    QFileInfoList entries = dir.entryInfoList();
    int size = entries.size();
    for (int i = 0; i < size; ++i)
    {
        QFileInfo entry = entries.at(i);
        QDir dest(psDest);
        QString path = dest.absoluteFilePath(entry.fileName());
        if (entry.isDir())
        {
            if (!cpDir(qPrintable(entry.absolutePath()), qPrintable(path)))
                return false;
        }
        else
        {
            if (!cpFile(qPrintable(entry.absoluteFilePath()), qPrintable(path)))
                return false;
        }
    }
    return true;
}

bool Utility::isFile(const char* psPath)
{
    QFileInfo info(psPath);
    return info.isFile();
}

bool Utility::rmFile(const char* psPath)
{
    return QFile::remove(psPath);
}

bool Utility::mvFile(const char* psPath, const char* psDest)
{
    if (cpFile(psPath, psDest))
        return rmFile(psPath);
    return false;
}

bool Utility::cpFile(const char* psPath, const char* psDest)
{
    return QFile::copy(psPath, psDest);
}


} // namespace ShTo
